package com.androwit.engine.mapping.converter.appender;

import org.apache.commons.beanutils.BeanUtils;

import com.androwit.engine.mapping.MappingField;
import com.androwit.engine.mapping.OneSensValueConverter;

/**
 * combines multiple target values into one java side field
 * 
 * @author Zied Hamdi Founder of http://1vu.fr, Zied Hamdi
 * 
 * @param <J>
 * @param <V>
 */
public abstract class ValueAppenderConverter<J, V> implements OneSensValueConverter<J, V> {

	@SuppressWarnings("unchecked")
	@Override
	public J setFromTarget(V value, Object instance, MappingField<J, ?, V> field) throws Exception {
		J originalValue = (J) BeanUtils.getProperty(instance, field.getName());
		J toSet = append(originalValue, value);
		BeanUtils.setProperty(instance, field.getName(), toSet);
		return toSet;
	}

	protected abstract J append(J originalValue, V value);

}
